package com.jeff.tianti.controller;

import com.alibaba.fastjson.JSONObject;
import com.jeff.tianti.cms.entity.MemberOf;
import com.jeff.tianti.cms.entity.Message;
import com.jeff.tianti.cms.service.MemberOfService;
import com.jeff.tianti.cms.service.MessageService;
import com.jeff.tianti.common.SMS.SmsSendUtils;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.enmus.SmsTemplate;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/message")
@Controller
public class MessageController {

    @Autowired
    private MemberOfService memberOfService;
    @Autowired
    private MessageService messageService;

    @RequestMapping("/transition")
    public String transition(HttpServletRequest request, Model model) {
        String str = request.getParameter("str");
        model.addAttribute("str", str);
        return "user/message/message_edit";
    }

    @RequestMapping("/sendMessage")
    @ResponseBody
    public AjaxResult sendMessage(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String str = request.getParameter("str");//拼接的id
            String title = request.getParameter("title");
            String content = request.getParameter("content");
            List<String> list = new ArrayList<>();
            if (StringUtils.isNotBlank(str)) {
                String[] split = str.split(",");
                list = Arrays.asList(split);
            }
            List<MemberOf> memberList = memberOfService.findAllByIds(list);
            List<String> phones = memberList.stream().map(c -> c.getPhone()).collect(Collectors.toList());
            //去重
            HashSet<String> set = new HashSet<>(phones);
            List<String> list1 = new ArrayList<>();
            list1.addAll(set);
            String state = request.getParameter("state");//1->短信发送，0->站内信发送
            if (list1 != null && list1.size() <= 0) {
                if (Constants.TEXT_MESSAFING.equals(state)) {//短信发送
                    textMessaging(list1, title, content);
                } else {
                    internalMailDelivery(list1, title, content);
                }
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @Autowired
    private SmsSendUtils smsSendUtils;

    private void textMessaging(List<String> phones, String title, String content) {//短信发送
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("storname", title);
        jsonObject.put("orderno", content);
        String jsoncode = jsonObject.toJSONString();
//        phones.stream().forEach(t -> smsSendUtils.sendJFSCbuyer(SmsTemplate.TemplateCode_buyer_notification, t, jsoncode));
    }

    private void internalMailDelivery(List<String> phones, String title, String content) {//站内发送
        Message message = new Message();
        phones.stream().forEach(t -> {
            message.setTitle(title);
            message.setContent(content);
            message.setPhone(t);
            messageService.save(message);
        });
    }

}
